//============================================================================
// Copyright (C) 2013 Brett R. Jones
//
// Issued to public domain 2013
//
// brettjones1900@gmail.com
// http://www.p2panarchy.com
//============================================================================

#include "VxConnectInfo.h"
#include "VxNetIdentBase.h"
#include "VxStringUtil.h"
#include "VxGlobals.h"

#include <memory.h>
#include <stdio.h>

//============================================================================
//=== constructor ===//
VxNetIdentBase::VxNetIdentBase()
	: m_u8OsAndPaidFlags(0)
	, m_u8OfferCnt(0)						// offer count ( used as part of rating )
	, m_u8ReplyCnt(0)						// reply count ( used as part of rating )
{
#if defined( WIN_PLATFORM )
	m_u8OsAndPaidFlags = OS_WINDOWS_FLAG;
#elif defined( ANDROID_PLATFORM ) 
	m_u8OsAndPaidFlags = OS_ANDROID_FLAG;
#else
	m_u8OsAndPaidFlags = OS_LINUX_FLAG;
#endif
	setIsPaidUser( VxIsPaidApp() );

	memset( m_as8OnlineName, 0, sizeof( m_as8OnlineName ) );
}

//============================================================================
void VxNetIdentBase::setIsPaidUser( bool isPaid )
{
	if( isPaid )
	{
		m_u8OsAndPaidFlags |= IS_APP_PAID_FLAG;
	}
	else
	{
		m_u8OsAndPaidFlags &= ~IS_APP_PAID_FLAG;
	}
}

//============================================================================
bool VxNetIdentBase::isPaidUser( void )
{
	return ( m_u8OsAndPaidFlags & IS_APP_PAID_FLAG ) ? true : false;
}

//============================================================================
//! return users online name
char *	VxNetIdentBase::getOnlineName( void )							{ return m_as8OnlineName; }

//============================================================================
//! return users online name as string
void	VxNetIdentBase::getOnlineName( std::string& strRetName )		{ strRetName = m_as8OnlineName; }

//============================================================================
//! set users online name
void 	VxNetIdentBase::setOnlineName( const char * pUserName )			{ VxSafeStrCopy( m_as8OnlineName, pUserName, sizeof( m_as8OnlineName )); }

//============================================================================
//! return true if have udp contact
bool	VxNetIdentBase::isFriendNearby( void )				{ return m_oUdpIp.isValid(); }

//============================================================================
//! return true if identity matches
bool	VxNetIdentBase::isVxNetIdentMatch( const VxNetIdentBase& oOtherIdent ) const
{
	return ( *((OnlineId *)&oOtherIdent.m_oDirectConnectId) == *((OnlineId *)&m_oDirectConnectId));
}

//============================================================================
void VxNetIdentBase::useAsRelay( VxNetIdentBase * poRelayIdent )
{
	m_oRelayConnectId = poRelayIdent->m_oDirectConnectId;
}

//============================================================================
//! equals operator
bool VxNetIdentBase::operator ==( const VxNetIdentBase &a ) const
{
	return this->isVxNetIdentMatch( a );
}

//============================================================================
void VxNetIdentBase::getProfileUri( std::string& retUri )
{
	std::string onlineId;
	U16 u16Port;
	VxInetAddress addr;
	if( hasRelay() && requiresRelay() )
	{
		u16Port = getRelayPort();
		addr = getRelayIpAddress();
	}
	else
	{
		u16Port = getOnlinePort();
		addr = getOnlineIpAddress();
	}
	char as8Port[ 20 ];
	sprintf( as8Port, "%d", u16Port );
	std::string ipAddr = addr.toStdString();
	getMyOnlineId( onlineId );

	retUri = "http://";
	retUri += ipAddr;
	retUri += ":";
	retUri += as8Port;
	retUri += "/";
	retUri += onlineId;
	retUri += "/index.htm";
}

//============================================================================
//! not equal operator
bool VxNetIdentBase::operator != ( const VxNetIdentBase &a ) const
{
	return !(this->isVxNetIdentMatch( a ));
}

//============================================================================
//! assignment operator
VxNetIdentBase& VxNetIdentBase::operator =( const VxNetIdentBase &a )
{
	// Check for self-assignment!
	if (this == &a)      // Same object?
		return *this;        // Yes, so skip assignment, and just return *this.
	// we can get away with memcpy because no virtual functions
	memcpy( this, &a, sizeof( VxNetIdentBase ) );
	return *this;
}
